package com.spring.appservice.web.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.spring.appservice.web.dao.AppQuestMapper;
import com.spring.appservice.web.model.Baby;
import com.spring.appservice.web.model.CommitExamGetNext;
import com.spring.appservice.web.model.Quest;
import com.spring.appservice.web.service.AppQuestService;
import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;

/**
 * Created by wojunyu on 2015/7/26.
 */
@Service
public class AppQuestServiceImpl implements AppQuestService {
	@Resource
	private AppQuestMapper appQuestMapper;

	@Resource
	private JedisCache jedisCache;

	@Override
	public List<Quest> selectQuestByType(Map m) {
		return appQuestMapper.selectQuestByType(m);
	}

	@Override
	public List<Quest> selectExamQuestById(Map m) {
		return appQuestMapper.selectExamQuestById(m);
	}

	@Override
//	@Cacheable(value={"questcache"}, key="#m.rule_id")
	public List selectRuleDetail(Map m) {
		return appQuestMapper.selectRuleDetail(m);
	}

	@Override
	public ArrayList selectgetExamHistory(String user_id, String baby_id,
			long skip, long rows) {
		// page_size*(page_index-1)+1;
		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("user_id", user_id);
		hm.put("baby_id", baby_id);
		// System.out.println(page_size * (page_index - 1));
		// System.out.println(page_size * page_index - 1);
		// hm.put("page_begin", page_size * (page_index - 1));
		// hm.put("page_end", page_size * page_index - 1);
		hm.put("page_begin", skip);
		hm.put("page_end", skip + rows);
		String count = appQuestMapper.getExamHistoryCountById(hm);

		ArrayList a = new ArrayList();
		a.add(0, count);
		a.add(1, appQuestMapper.getExamHistoryById(hm));

		return a;

	}

	@Override
	public List<Quest> getExamHistoryDetail(String score_id) {
		System.out.println(score_id + "service");
		return appQuestMapper.getExamHistoryDetail(score_id);
	}

	@Override
	public List<Quest> getExamHistoryDetailComplete(String score_id,
			String quest_type_id) {
		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("score_id", score_id);
		hm.put("quest_type_id", quest_type_id);
		return appQuestMapper.getExamHistoryDetailComplete(hm);
	}

	@Override
	public int insertExam(Map m) {
		return appQuestMapper.insertExam(m);
	}

	@Override
	public int insertExamQuestions(Map m) {
		return appQuestMapper.insertExamQuestions(m);
	}

	@Override
	public String CreateExamByRule(String UID, String rule_setting_id,
			String exam_title, List<Map> ruleList, String user_id,
			String c_baby_id) {
		// 先生成试卷
		Map<String, Object> exam = new HashMap<String, Object>();
		exam.put("id", UID);// 主键
		exam.put("t_user_id", "");// 教师
		exam.put("exam_title", exam_title);
		exam.put("rule_setting_id", rule_setting_id);
		exam.put("c_user_id", user_id);// 操作人 u.getId()
		exam.put("c_baby_id", c_baby_id);
		appQuestMapper.insertExam(exam);
		// 按规则随即获取每元题目
		Map<String, Object> questM = new HashMap<String, Object>();
		questM.put("exam_info_id", UID);
		int quest_index = 0;
		for (Map q_m : ruleList) {
			String question_typ_key = JedisCont.QUESTION_KEY + "_"
					+ q_m.get("question_type_id") + "_"
					+ q_m.get("rule_age_type_id");
			if (jedisCache.llen(question_typ_key) <= 0) {
				checkQuestionRedis(q_m.get("question_type_id") + "",
						question_typ_key, ApplicationUtils.Object2String(q_m
								.get("rule_age_type_id")));
			}

			System.out.println("length---------->"+jedisCache.llen(question_typ_key));
			
			// 根据key获取每元题目生成试卷
			int[] set = ApplicationUtils.randomArray(
					1,
					Integer.parseInt((long)jedisCache.llen(question_typ_key)+""),
					Integer.parseInt((long)jedisCache.llen(question_typ_key)+"") > (Integer) q_m
							.get("title_number") ? (Integer) q_m
							.get("title_number") : Integer.parseInt((long) jedisCache
							.llen(question_typ_key)+""));
			for (int j : set) {
				quest_index++;
				String listValue = jedisCache.lindex(question_typ_key, j - 1);
				String[] info = listValue.split(",");
				questM.put("id", ApplicationUtils.randomUUID());
				questM.put("question_index", quest_index);
				questM.put("question_id", info[0]);
				questM.put("question_type_id", info[1]);
				appQuestMapper.insertExamQuestions(questM);
			}
		}
		return (quest_index) + "";
	}

	// 答题提交并生成下一题
	@Override
	public List<CommitExamGetNext> commitExam(String c_user_id,String c_baby_id,String exam_id,
			String question_index, String question_id, String user_option_id,
			String next_index) {
		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("exam_id", exam_id);
		hm.put("question_index", question_index);
		//判断是否已经有记录了答题记录了
		String s = appQuestMapper.commitExamCheckHaveDate(hm);

		if (s == null) {

			HashMap<String,Object> hm2 = new HashMap<String,Object>();

			hm2.put("exam_id", exam_id);
			hm2.put("question_index", question_index);
			hm2.put("question_id", question_id);
			hm2.put("next_index", next_index);
			hm2.put("user_option_id", user_option_id);
			hm2.put("uuid", ApplicationUtils.randomUUID());
			// 需要添加c_user_id,c_baby_id  插入答题记录  exam_answer
			appQuestMapper.commitExamInsertExam_answer(hm2);

		} else {
			HashMap<String,Object> hm3 = new HashMap<String,Object>();

			hm3.put("exam_answer_id", s);
			hm3.put("user_option_id", user_option_id);
			//更新答题记录，重新做题
			appQuestMapper.commitExamInsertUpdateExam_answer(hm3);

		}

		HashMap<String,Object> hm4 = new HashMap<String,Object>();
		hm4.put("exam_id", exam_id);
		hm4.put("next_index", next_index);
		//获取下一题
		return appQuestMapper.commitExamGetNext(hm4);
	}

	/**
	 * 检查题库,如果题库中题目为0,则更新题库,将8元题目按8元类型放入题库
	 * 
	 * @param question_type_id
	 * @param question_type_key
	 */
	private void checkQuestionRedis(String question_type_id,
			String question_type_key, String rule_age_type_id) {
//		System.out.println("checkQuestionRedis/begin:"+UDate.getDate("yyyy-MM-dd HH:mm:ss"));
		if (jedisCache.llen(question_type_key) <= 0) {
			Map<String, Object> m = new HashMap<String, Object>();
			m.put("question_type_id", question_type_id);
			m.put("rule_age_type_id", rule_age_type_id);
			List<Quest> questList = appQuestMapper.selectQuestByType(m);
			for (Quest quest : questList) {
				jedisCache.lpush(question_type_key, quest.getQuestion_id()
						+ "," + quest.getQuestion_type_id());
			}
			if (jedisCache.exists(question_type_key)
					&& !"".equalsIgnoreCase(question_type_key)) {
				jedisCache.expire(question_type_key, 3600);
			}
		}
//		System.out.println("checkQuestionRedis/end:"+UDate.getDate("yyyy-MM-dd HH:mm:ss"));
	}

	// 改中。。。。。。。。。。。。。。
	// 得到历史答题记录
	public ArrayList getNotCompleteExamHistory(String exam_id) {
		// List<CommitExamGetNext>
		// System.out.println(appQuestMapper.getNotCompleteExamHistory(exam_id).size()
		// + "size");
		// System.out.println(appQuestMapper.getNotCompleteExamHistory(exam_id).toString());

		ArrayList a = new ArrayList();
		// a.add(0,);
		String s = appQuestMapper.getNotCompleteExamCountHistory(exam_id);

		List<CommitExamGetNext> list = appQuestMapper
				.getNotCompleteExamHistory(exam_id);
		a.add(0, s);
		a.add(1, list);

		return a;
	}

	// 提交答案
	public int commitExamAll(String exam_id) {

		HashMap<String,Object> hm = new HashMap<String,Object>();
		hm.put("status", "");
		hm.put("exam_id", exam_id);

		appQuestMapper.commitExamAll(hm);
		int a = (Integer) hm.get("status");
		System.out.println(a);

		return a;
	}

	@Override
	public List<Baby> getBabyByParent(String c_user_id) {
		// TODO Auto-generated method stub
		return appQuestMapper.getBabyByParent(c_user_id);
	}

}
